<template>
  <van-nav-bar
    title="新增地址"
    left-text="返回"
    left-arrow
    @click-left="onClickLeft"
  />

  <van-address-edit
    :area-list="areaList"
    show-set-default
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
  />
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { showToast } from 'vant'
import { areaList } from '@vant/area-data';
import { addressApi } from '@/api/index'
import { useRoute, useRouter } from 'vue-router'
import {appStore} from '@/store/appStore'
import { storeToRefs } from "pinia"
let { userId, phone } = storeToRefs(appStore())

const route = useRoute()   //不带r的是获取当前页面url相关参数
const router = useRouter()  //代r的是负责页面跳转


onMounted(() => {


})

const onSave = (info: any) => {
    let isDefault=1
    if(info.isDefault===true){
        isDefault=0
    }

    const params = {
        name: info.name,
        phone: info.tel,
        province: info.province,
        city: info.city,
        country: info.county,
        areaCode: info.areaCode,
        addressDetail: info.addressDetail,
        isDefault: isDefault,
        userId: userId.value
    }

    addressApi.add.call(params).then((res: any) => {
        showToast("保存成功")
        router.push({name:"address-list"})
    })

};

const onClickLeft = () => history.back();
</script>
<style scoped>

</style>
